package com.skynet.supervision.utils;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Map;

public abstract class CollectionExtUtils extends CollectionUtils {

    public static boolean notEmpty(Collection<?> collection) {
        return !isEmpty(collection);
    }

    public static boolean notEmpty(Map<?, ?> collection) {
        return !isEmpty(collection);
    }

    /**
     * * 判断一个对象数组是否非空
     *
     * @param objects 要判断的对象数组
     * @return true：非空 false：空
     */
    public static boolean notEmpty(Object[] objects)
    {
        return !isEmpty(objects);
    }
    public static boolean isEmpty(Object[] objects)
    {
        return ObjectUtil.isNull(objects) || (objects.length == 0);
    }
    /**
     * take first element of collection
     *
     * @param collection collection
     * @return null if collection is null or either empty
     */
    public static <T> T first(Collection<T> collection) {
        if (isEmpty(collection)) {
            return null;
        }
        return collection.iterator().next();
    }

    @Nonnull
    public static <T> T nonNullFirst(Collection<T> collection) {
        if (isEmpty(collection)) {
            throw new NullPointerException("collection is null or empty");
        }
        return collection.iterator().next();
    }


    public static int sizeOf(Map<?, ?> m) {
        if (notEmpty(m)) {
            return m.size();
        }
        return 0;
    }

    public static int sizeOf(Collection<?> c) {
        if (notEmpty(c)) {
            return c.size();
        }
        return 0;
    }

    /**
     * 判断给定的set列表中是否包含数组array 判断给定的数组array中是否包含给定的元素value
     *
     * @param collection 给定的集合
     * @param array 给定的数组
     * @return boolean 结果
     */
    public static boolean containsAnyStr(Collection<String> collection, String... array)
    {
        if (isEmpty(collection) || isEmpty(array))
        {
            return false;
        }
        else
        {
            for (String str : array)
            {
                if (collection.contains(str))
                {
                    return true;
                }
            }
            return false;
        }
    }
    /**
     * 查找指定字符串是否包含指定字符串列表中的任意一个字符串同时串忽略大小写
     *
     * @param cs 指定字符串
     * @param searchCharSequences 需要检查的字符串数组
     * @return 是否包含任意一个字符串
     */
    public static boolean containsAnyIgnoreCase(CharSequence cs, CharSequence... searchCharSequences)
    {
        if (Strings.isEmpty(cs) || isEmpty(searchCharSequences))
        {
            return false;
        }
        for (CharSequence testStr : searchCharSequences)
        {
            if (Strings.containsIgnoreCase(cs, testStr))
            {
                return true;
            }
        }
        return false;
    }
}
